using MediatR;
using Microsoft.AspNetCore.Mvc;
using TerritoryGame.Application.Commands;

namespace TerritoryGame.API.Controllers;

[ApiController]
[Route("api/commands")]
public class CommandController : ControllerBase
{
    private readonly IMediator _mediator;
    
    public CommandController(IMediator mediator)
    {
        _mediator = mediator;
    }
    
    [HttpPost("create-room")]
    public async Task<ActionResult<CreateRoomResult>> CreateRoom([FromBody] CreateRoomCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    [HttpPost("join-room")]
    public async Task<ActionResult<JoinRoomResult>> JoinRoom([FromBody] JoinRoomCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    [HttpPost("start-game")]
    public async Task<ActionResult<StartGameResult>> StartGame([FromBody] StartGameCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    [HttpPost("restart-game")]
    public async Task<ActionResult<RestartGameResult>> RestartGame([FromBody] RestartGameCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    [HttpPost("end-game")]
    public async Task<ActionResult<EndGameResult>> EndGame([FromBody] EndGameCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    [HttpPost("paint-action")]
    public async Task<ActionResult<PaintActionResult>> PaintAction([FromBody] PaintActionCommand command)
    {
        var result = await _mediator.Send(command);
        if (!result.Success)
        {
            return BadRequest(result);
        }
        return Ok(result);
    }
    
    // [HttpPost("disband-room")]
    // public async Task<ActionResult<DeleteRoomResult>> DisbandRoom([FromBody] DeleteRoomCommand command)
    // {
    //     Console.WriteLine($"DisbandRoom called with RoomId: {command.RoomId}, RoomCode: {command.RoomCode}");
        
    //     var result = await _mediator.Send(command);
        
    //     if (!result.Success)
    //     {
    //         Console.WriteLine($"Failed to disband room: {result.ErrorMessage}");
    //         return BadRequest(result);
    //     }
        
    //     Console.WriteLine($"Successfully disbanded room: {command.RoomId}");
    //     return Ok(result);
    // }
    [HttpPost("delete-room")]
public async Task<IActionResult> DeleteRoom([FromBody] DeleteRoomDto dto)
{
    await _mediator.Send(new DeleteRoomCommand { RoomCode = dto.RoomCode });
    return Ok();
}

[HttpPost("disband-room")]
public async Task<IActionResult> DisbandRoom([FromBody] DisbandRoomDto dto)
{
    if (string.IsNullOrWhiteSpace(dto.RoomCode))
        return BadRequest(new { message = "RoomCode不能为空" });

    await _mediator.Send(new DeleteRoomCommand { RoomCode = dto.RoomCode });
    return Ok();
}

public class DeleteRoomDto
{
    public string RoomCode { get; set; }
}

public class DisbandRoomDto
{
    public string RoomCode { get; set; }
}
}
